#include "settings.h"

#include <QDebug>
#include <QSettings>
#include <QCoreApplication>

Settings* Settings::m_instance = 0;

#define PROJECTPATH "projPath"
#define SERVERSLIST "servList"

Settings::Settings() :
    QObject()
{
    load();
}

Settings::~Settings()
{
}

Settings* Settings::instance()
{
    if(m_instance == 0)
        m_instance = new Settings();

    return m_instance;
}

void Settings::load()
{
#ifdef Q_OS_ANDROID
    QSettings settings("/mnt/sdcard/.uiscripter/settings.ini", QSettings::IniFormat);
#else
    QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
#endif
    m_projectPath = settings.value(PROJECTPATH).toString();
    m_serversList = settings.value(SERVERSLIST).toStringList();
}

void Settings::save()
{
#ifdef Q_OS_ANDROID
    QSettings settings("/mnt/sdcard/.uiscripter/settings.ini", QSettings::IniFormat);
#else
    QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
#endif
    settings.setValue(PROJECTPATH, m_projectPath);
    settings.setValue(SERVERSLIST, m_serversList);
}

QString Settings::serverHost(int index)
{
    if(index < m_serversList.size())
        return  m_serversList.at(index).split(':').at(0);
    else
        return QString();
}

int Settings::serverPort(int index)
{
    if(index < m_serversList.size())
        return  m_serversList.at(index).split(':').at(1).toInt();
    else
        return 0;
}

